/*
 * Copyright (c) 2017, www.zm0618.com All Rights Reserved..
 */

package com.light.hessian.provider.discovery;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * Created by zhaojun on 2017/02/10.
 */
public class ProviderRegistryLocalConfigParser implements ProviderRegistryConfParser{
    @Override
    public void doParse(AbstractProviderDiscoverer discoverer) {

        /**
         yxs.provider.discovery.type=local          服务提供者发现方式 可选
         yxs.provider.client.balance=roundrobin     客户端负载均衡方式  可选
         yxs.provider.nodes=192.168.10.21:8080,192.168.10.21:8081   服务提供者节点   必选
         yxs.provider.transport.protocol=http                       传输协议        可选
         yxs.provider.path.prefix=/rpc                              路径前缀        可选
         */
        Properties registryConfProperties = discoverer.getRegistryConfProperties();
        String provider= discoverer.getProvider();
        String nodes=registryConfProperties.getProperty(String.format("%s.provider.nodes",provider));
        String []node= StringUtils.split(nodes,",");
        List<String> nodeList=new ArrayList<String>();
        Collections.addAll(nodeList,node);
        String protocol=registryConfProperties.getProperty(String.format("%s.provider.transport.protocol",provider),"http");   //默认为http协议
        String pathPrefix=registryConfProperties.getProperty(String.format("%s.provider.path.prefix",provider),"/");          //默认为/
        String showBalancer=registryConfProperties.getProperty(String.format("%s.client.balancer.show",provider),"false");

//        if (StringUtils.equals(discoverer.getProtocol(),"hessian")){//编码协议
//        }
        HessianLocalDiscoverReference localDiscoverReference=new HessianLocalDiscoverReference();
        localDiscoverReference.setPathPrefix(pathPrefix);
        localDiscoverReference.setProtocol(protocol);
        localDiscoverReference.setNodes(nodeList);
        localDiscoverReference.setProvider(provider);
        localDiscoverReference.setShowBalancer(Boolean.valueOf(showBalancer));
        discoverer.handleDiscoveryReference(localDiscoverReference);
    }
}
